﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace Gronia.WPF.Controls
{
    public partial class HyperPanel : Panel
    {
        #region Orientation
        public static readonly DependencyProperty OrientationProperty = StackPanel.OrientationProperty.AddOwner(typeof(HyperPanel));
        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }
        #endregion

        #region Effect Type
        public static readonly DependencyProperty EffectTypeProperty = DependencyProperty.Register("EffectType", typeof(HyperPanelEffectType), typeof(HyperPanel), new FrameworkPropertyMetadata(HyperPanelEffectType.Mouse, new PropertyChangedCallback(OnEffectTypeChanged)));
        public HyperPanelEffectType EffectType
        {
            get { return (HyperPanelEffectType)GetValue(EffectTypeProperty); }
            set { SetValue(EffectTypeProperty, value); }
        }

        private static void OnEffectTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            HyperPanel panel = (HyperPanel)sender;
            if (panel.EffectType == HyperPanelEffectType.Child)
            {
                OnHyperChildPropertyValueChanged(panel, new DependencyPropertyChangedEventArgs());
            }
        }
        #endregion

        #region Center
        public static readonly DependencyProperty CenterProperty = DependencyProperty.Register("Center", typeof(double), typeof(HyperPanel), new FrameworkPropertyMetadata(0.5, FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(delegate(object tt) { double t = (double)tt; return t >= 0 && t <= 1; }));
        public double Center
        {
            get { return (double)GetValue(CenterProperty); }
            set { SetValue(CenterProperty, value); }
        }
        #endregion

        #region HyperChild
        public static readonly DependencyProperty HyperChildProperty = DependencyProperty.Register("HyperChild", typeof(UIElement), typeof(HyperPanel), new PropertyMetadata(new PropertyChangedCallback(OnHyperChildPropertyValueChanged)));
        public UIElement HyperChild
        {
            get { return (UIElement)GetValue(HyperChildProperty); }
            set { SetValue(HyperChildProperty, value); }
        }

        private static void OnHyperChildPropertyValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            HyperPanel panel = (HyperPanel)sender;
            if (panel.HyperChild != null)
            {
                int index = panel.Children.IndexOf(panel.HyperChild);
                int count = panel.Children.Count;
                double o = panel.Orientation == Orientation.Horizontal ? panel.ActualWidth : panel.ActualHeight;
                if (o == 0)
                {
                    o = panel.Orientation == Orientation.Horizontal ? panel.Width : panel.Height;
                }
                if (index >= 0)
                {
                    double per = o / count;
                    double center = per * index + per / 2;
                    panel.Center = center / o;
                }
            }
        }
        #endregion

        #region ApplyOpacity
        public static readonly DependencyProperty ApplyOpacityProperty = DependencyProperty.Register("ApplyOpacity", typeof(bool), typeof(HyperPanel), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsMeasure));
        public bool ApplyOpacity
        {
            get { return (bool)GetValue(ApplyOpacityProperty); }
            set { SetValue(ApplyOpacityProperty, value); }
        }
        #endregion

        #region MinimumScale
        public static readonly DependencyProperty MinimumScaleProperty = DependencyProperty.Register("MinimumScale", typeof(double), typeof(HyperPanel), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsMeasure));
        public double MinimumScale
        {
            get { return (double)GetValue(MinimumScaleProperty); }
            set { SetValue(MinimumScaleProperty, value); }
        }
        #endregion

        #region Distribution
        public static readonly DependencyProperty DistributionProperty = DependencyProperty.Register("Distribution", typeof(double), typeof(HyperPanel), new FrameworkPropertyMetadata(0.1, FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(delegate(object tt) { double t = (double)tt; return t >= 0 && t <= 1; }));
        public double Distribution
        {
            get { return (double)GetValue(DistributionProperty); }
            set { SetValue(DistributionProperty, value); }
        }
        #endregion

        #region TempOpacity
        private static readonly DependencyProperty TempOpacityProperty = DependencyProperty.RegisterAttached("TempOpacity", typeof(double?), typeof(HyperPanel), new PropertyMetadata(null));
        private static void SetTempOpacity(DependencyObject obj, double? value)
        {
            obj.SetValue(TempOpacityProperty, value);
        }
        private static double? GetTempOpacity(DependencyObject obj)
        {
            return (double?)obj.GetValue(TempOpacityProperty);
        }
        #endregion
    }
}